﻿namespace Encrypt
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Security.Cryptography;
    using System.Text;

    public static class SecurityExtension
    {
       

        public static string Encrypt(this string Text)
        {
            string s = "ng=ycwd!";
            string str2 = "";
            if (Text.Trim() != "")
            {
                if (s.Trim().Length != 8)
                {
                    return str2;
                }
                byte[] rgbKey = null;
                byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
                try
                {
                    rgbKey = Encoding.UTF8.GetBytes(s);
                    DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                    byte[] bytes = Encoding.UTF8.GetBytes(Text);
                    MemoryStream stream = new MemoryStream();
                    CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                    stream2.Write(bytes, 0, bytes.Length);
                    stream2.FlushFinalBlock();
                    str2 = Convert.ToBase64String(stream.ToArray());
                }
                catch (Exception)
                {
                    return "";
                }
            }
            return str2;
        }

        public static string Decrypt(this string Text)
        {
            string str = "";
            byte[] rgbKey = null;
            byte[] rgbIV = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };
            byte[] buffer = new byte[Text.Length];
            try
            {
                rgbKey = Encoding.UTF8.GetBytes("ng=ycwd!");
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                buffer = Convert.FromBase64String(Text);
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                str = new UTF8Encoding().GetString(stream.ToArray());
            }
            catch (Exception)
            {
                return "";
            }
            return str;
        }
    }
}

